home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / C / Mesa / samples / oglinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  5.3 KB  |  212 lines

  1. /* oglinfo.c */
  2.  
  3. /* This demo modified by BrianP to accomodate Mesa and test the GLX 1.1 functions. */
  4.  
  5.  
  6.  
  7. #include <GL/glx.h>
  8. #include <GL/gl.h>
  9. #include <GL/glu.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. int visual_request[] = { None }; /* don't need much of a visual */
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.   char *display_name = NULL;
  18.   char *string;
  19.   Display       *dpy;
  20.   int           screen_num;
  21.   int           major, minor;
  22.   XVisualInfo   *vis;
  23.   GLXContext    ctx;
  24.   Window        root,  win;
  25.   Colormap      cmap;
  26.   XSetWindowAttributes swa;
  27.   int dontcare;
  28.  
  29.   /* parse arguments */
  30.   if(argc > 1)
  31.     if(!strcmp(argv[1],"-display"))
  32.       display_name = argv[2];
  33.     else {
  34.       fprintf(stderr, "Usage: %s [-display <display>]\n",argv[0]);
  35.       return -1;
  36.     }
  37.  
  38.   /* get display */
  39.   if (!(dpy = XOpenDisplay(display_name))) {
  40.     fprintf(stderr,"Error: XOpenDisplay() failed.\n");
  41.     return -1;
  42.   }
  43.  
  44.   /* does the server know about OpenGL & GLX? */
  45. #ifndef MESA
  46.   if(!XQueryExtension(dpy, "GLX", &dontcare, &dontcare, &dontcare)) {
  47.     fprintf(stderr,"This system doesn't appear to support OpenGL\n");
  48.     return -1;
  49.   }
  50. #endif
  51.  
  52.   /* find the glx version */
  53.   if(glXQueryVersion(dpy, &major, &minor))
  54.     printf("GLX Version: %d.%d\n", major, minor);
  55.   else {
  56.     fprintf(stderr, "Error: glXQueryVersion() failed.\n");
  57.     return -1;
  58.   }
  59.  
  60.   /* get screen number */
  61.   screen_num = DefaultScreen(dpy);
  62.  
  63. /* This #ifdef isn't redundant. It keeps the build from breaking
  64. ** if you are building on a machine that has an old (1.0) version
  65. ** of glx.
  66. **
  67. ** This program could still be *run* on a machine that has an old 
  68. ** version of glx, even if it was *compiled* on a version that has
  69. ** a new version.
  70. **
  71. ** If compiled on a system with an old version of glx, then it will 
  72. ** never recognize glx extensions, since that code would have been
  73. ** #ifdef'ed out.
  74. */
  75. #ifdef GLX_VERSION_1_1
  76.  
  77.   /*
  78.   ** This test guarantees that glx, on the display you are inquiring,
  79.   ** suppports glXQueryExtensionsString().
  80.   */
  81.   if(minor > 0 || major > 1)
  82.     string = (char *) glXQueryExtensionsString(dpy, screen_num);
  83.   else
  84.     string = "";
  85.  
  86.   if(string)
  87.     printf("GLX Extensions (client & server): %s\n",
  88.        string);
  89.   else {
  90.     fprintf(stderr, "Error: glXQueryExtensionsString() failed.\n");
  91.     return -1;
  92.   }
  93.  
  94.   if (minor>0 || major>1) {
  95.      printf("glXGetClientString(GLX_VENDOR): %s\n", glXGetClientString(dpy,GLX_VENDOR));
  96.      printf("glXGetClientString(GLX_VERSION): %s\n", glXGetClientString(dpy,GLX_VERSION));
  97.      printf("glXGetClientString(GLX_EXTENSIONS): %s\n", glXGetClientString(dpy,GLX_EXTENSIONS));
  98.      printf("glXQueryServerString(GLX_VENDOR): %s\n", glXQueryServerString(dpy,screen_num,GLX_VENDOR));
  99.      printf("glXQueryServerString(GLX_VERSION): %s\n", glXQueryServerString(dpy,screen_num,GLX_VERSION));
  100.      printf("glXQueryServerString(GLX_EXTENSIONS): %s\n", glXQueryServerString(dpy,screen_num,GLX_EXTENSIONS));
  101.   }
  102.  
  103.  
  104. #endif
  105.  
  106.    /* get any valid OpenGL visual */
  107.    if (!(vis = glXChooseVisual(dpy, screen_num, visual_request)))  {
  108.      fprintf(stderr,"Error: glXChooseVisual() failed.\n");
  109.      return -1;
  110.      }
  111.  
  112.    /* get context */
  113.    ctx = glXCreateContext(dpy,vis,0,GL_TRUE);
  114.  
  115.    /* root window */
  116.    root = RootWindow(dpy,vis->screen);
  117.  
  118.    /* get RGBA colormap */
  119.    cmap = XCreateColormap(dpy, root, vis->visual, AllocNone);
  120.  
  121.    /* get window */
  122.    swa.colormap = cmap;
  123.    swa.border_pixel = 0;
  124.    swa.event_mask = StructureNotifyMask;
  125.    win = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, vis->depth,
  126.                InputOutput,vis->visual,
  127.                CWBorderPixel|CWColormap|CWEventMask,
  128.                &swa);
  129.  
  130.    glXMakeCurrent(dpy,win,ctx);
  131.  
  132.   string = (char *) glGetString(GL_VERSION);
  133.   if(string)
  134. #ifdef MESA
  135.     printf("Mesa Version: %s\n", string);
  136. #else
  137.     printf("OpenGL Version: %s\n", string);
  138. #endif
  139.   else {
  140.     fprintf(stderr, "Error: glGetString(GL_VERSION) failed.\n");
  141.     return -1;
  142.   }
  143.  
  144.   string = (char *) glGetString(GL_EXTENSIONS);
  145.  
  146.   if(string)
  147. #ifdef MESA
  148.     printf("Mesa Extensions: %s\n", string);
  149. #else
  150.     printf("OpenGL Extensions: %s\n", string);
  151. #endif
  152.   else {
  153.     fprintf(stderr, "Error: glGetString(GL_EXTENSIONS) failed.\n");
  154.     return -1;
  155.   }
  156.  
  157.   string = (char *) glGetString(GL_RENDERER);
  158.  
  159.   if(string)
  160. #ifdef MESA
  161.     printf("Mesa Renderer: %s\n", string);
  162. #else
  163.     printf("OpenGL renderer: %s\n", string);
  164. #endif
  165.   else {
  166.     fprintf(stderr, "Error: glGetString(GL_RENDERER) failed.\n");
  167.     return -1;
  168.   }
  169.  
  170. /*
  171. ** This #ifdef prevents a build failure if you compile on an a
  172. ** machine with an old GLU library. 
  173. **
  174. ** If you build on a pre GLU 1.1 machine, you will never be able
  175. ** to get glu info, even if you run on a GLU 1.1 or latter machine,
  176. ** since the code has been #ifdef'ed out.
  177. */
  178. #ifdef GLU_VERSION_1_1
  179.  
  180.   /*
  181.   ** If the glx version is 1.1 or latter, gluGetString() is guaranteed
  182.   ** to exist.
  183.   */
  184.   if(minor > 0 || major > 1)
  185.     string = (char *) gluGetString(GLU_VERSION);
  186.   else
  187.     string = "1.0";
  188.  
  189.   if(string)
  190.     printf("GLU Version: %s\n", string);
  191.   else {
  192.     fprintf(stderr, "Error: gluGetString(GLU_VERSION) failed.\n");
  193.     return -1;
  194.   }
  195.   
  196.   if(minor > 0 || major > 1)
  197.     string = (char *) gluGetString(GLU_EXTENSIONS);
  198.   else
  199.     string = "";
  200.  
  201.   if(string)
  202.     printf("GLU Extensions: %s\n", string);
  203.   else {
  204.     fprintf(stderr, "Error: gluGetString(GLU_EXTENSIONS) failed.\n");
  205.     return -1;
  206.   }
  207.  
  208.  
  209. #endif
  210.   return 0;
  211. }
  212.